home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS15.ADF / C / Pr / pr.c < prev    next >
C/C++ Source or Header  |  1988-04-20  |  4KB  |  184 lines

  1. /* pr.c
  2.  
  3.     simple print file utility by: Bob Leivian
  4.  
  5.     pr [-chn] file [file2...]
  6.  
  7.     This program will print a list of files in a background cli
  8.  
  9.     This program will accept Un*x style wildcards, not Amigados
  10.     o * matches any substring
  11.     o ? matches any single character
  12.  
  13.     This program has options to...
  14.     o print line numbers (-n)
  15.     o print a header before each file (-h)
  16.     o confirm all wild card matches individulally (-c)
  17.         <CR> means yes, I want that file printed
  18.         ! means yes for this one and all remaining
  19.         anything else means no don't print the file
  20.     o options may be grouped: i.e. -chn, or separated: i.e. -h -n -c
  21.  
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. int control = 0;
  29. int confirm = 0;
  30.  
  31.  
  32. /* Amiga lib execute a cli command */
  33. long Execute();
  34. char *scdir();
  35. char *gets();
  36.  
  37. main(argc, argv)
  38. int argc;
  39. char *argv[];
  40. {
  41.     int i;
  42.     char *p;
  43.     FILE *out;
  44.     char buf[2048];        /* command buffer for print */
  45.     char name[30];        /* name of file that we are using this pass */
  46.     char answer[10];
  47.     char append[4];
  48.     char c;
  49.     int accepted;
  50.     long result;
  51.     int once = 0;
  52.  
  53.     printf("pr copyright(c) 1986 by Robert H. Leivian\n");
  54.     if (argc < 2) {
  55.         printf("usage: pr -chnpwl:<control string> file1 file2...\n");
  56.         printf("use pr -? for more help\n");
  57.         exit(27);
  58.     }
  59.  
  60.     /* set up to run the simple print in another cli */
  61.     strcpy(buf, "RUN print ");
  62.     strcpy(append, " -x");
  63.  
  64.     while (argv[1] != NULL) {
  65.  
  66.         /* process options if any */
  67.         while (*argv[1] == '-') {
  68.             p = (char *) &*argv[1];
  69.  
  70.             p++;        /* point to the option chars */
  71.             do {
  72.                 switch (*p) {
  73.                 case '^':        /* just pass these options to print */
  74.                 case 'n':
  75.                 case 'h':
  76.                 case 'w':
  77.                 case 'p':
  78.                 case 'l':
  79.                     append[2] = *p;
  80.                     strcat(buf, append);
  81.                     break;
  82.  
  83.                 case ':':        /* print the control char string first */
  84.                     strcat(buf, " -:^j");/* include a char for the line eater*/
  85.                     strcat(buf, p); /* get the control string goto next opt */
  86.                     goto next;
  87.  
  88.                 case 'c':        /* confirm any templates */
  89.                     confirm++;
  90.                     break;
  91.  
  92.                 case '?':
  93. printf("'pr' prints multiple files in background with serval options\n\n");
  94.                 goto help;
  95.  
  96.                 default:
  97. printf("'%c' is an invalid option, ", *p);
  98. help:
  99. printf("The vaild options are...\n");
  100. printf("n - line numbers, h -headers, ^ - print control chars as ^X\n");
  101. printf("l - letter quailty, w - wide (132 char mode), p - proportional\n");
  102. printf("c - confirm wild cards, :<string> - print string first\n");
  103.                 exit(0);
  104.                 }
  105.                 p++;
  106.             } while (*p);
  107.     next:
  108.             argc--;
  109.             argv++;
  110.         }
  111.  
  112.         strcat(buf, " ");
  113.  
  114.         /* must be a file name */
  115.         strcpy(name, argv[1]);
  116.  
  117.         if (index(name, '*') || index(name, '?')) {
  118.  
  119.             /* a template was given, expand it */
  120.             i = 0;
  121. #ifdef DEBUG
  122. printf("expanding template: %s\n", name);
  123. #endif
  124.             while (((p = scdir(name)) != NULL) && ++i < 100) {
  125.  
  126.                 /* if confirm ask him for each file */
  127.                 if (confirm) {
  128. again:
  129.                     printf("confirm '%s'? ", p);
  130.                     gets(answer);
  131.                     c = answer[0];
  132.                     switch (c) {
  133.                     case '!':
  134.                         /* if he answers ! then accept all remaining */
  135.                         confirm = 0;
  136.                         accepted = 1;
  137.                         break;
  138.  
  139.                     case '\0':
  140.                     case 'y':
  141.                     case 'Y':
  142.                         /* if yes then include this name */
  143.                         accepted = 1;
  144.                         break;
  145.  
  146.                     case 'N':
  147.                     case 'n':
  148.                         accepted = 0;
  149.                         break;
  150.  
  151.                     default:
  152.                         printf("valid answers are Yy<ret> - yes do print\n");
  153.                         printf("  Nn - no don't print this one\n");
  154.                         printf("  ! - yes print this one and all remaining\n");
  155.                         goto again;
  156.                     }
  157.                 } else
  158.                     /* if not confirming then match all  items */
  159.                     accepted = 1;
  160.  
  161.                 /* go ahead and print this one */
  162.                 if (accepted) {
  163.                     strcat(buf, p);
  164.                     strcat(buf, " ");
  165.                     printf("spooling: '%s'\n", p);
  166.                 }
  167.             }
  168.         } else {
  169.             printf("spooling: %s\n", name);
  170.             strcat(buf, name);
  171.             strcat(buf, " ");
  172.         }
  173.  
  174.         argv++;
  175.     }
  176. #ifdef DEBUG
  177. printf("command...\n%s\n", buf);
  178. #endif
  179.  
  180.     /* now just run the print in another CLI */
  181.     result = Execute(buf, 0L, 0L);
  182.     return result;
  183. }
  184.